package com.itcast.myrpc.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * @program: bio_tcp
 * @description: byteBuf的读取操作
 * @author: zhanghz001
 * @create: 2021-02-22 11:37
 **/
public class ByteBufTest01 {
    public static void main(String[] args) {
        //构建
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
        //查看相关信息
        System.out.println("byteBuf容量: " + byteBuf.capacity());
        System.out.println("byteBuf可写容量为: " + byteBuf.writableBytes());
        System.out.println("byteBuf可读容量为: " + byteBuf.readableBytes());
        
        //方法一:通过readIndex移动读取数据
        // while (byteBuf.isReadable()) {
        //     System.out.println((char) byteBuf.readByte());
        // }
        System.out.println("===================");
        //方法二:直接通过下标获取
        for (int i = 0; i < byteBuf.readableBytes(); i++) {
            System.out.println((char) byteBuf.getByte(i));
        }
        System.out.println("===================");
        //方法三:转换成byte[]进行读取
        byte[] array = byteBuf.array();
        for (byte b : array) {
            System.out.println((char) b);
        }
    }
}
